package com.liuxu.topnews.utils;

import android.content.Context;
import android.os.Environment;
import android.util.Log;
import android.util.Xml;

import com.liuxu.topnews.MyApplication;
import com.liuxu.topnews.bean.App;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Map;

public class SettingUtils {


    public ArrayList<App> GetDefaultXml(){
        ArrayList<App> defaultapps=GetXml(!fileIsExists());
        return defaultapps;
    }

    public ArrayList<App> GetXml(boolean defaultSetting){
        XmlPullParser xpp = Xml.newPullParser();
        ArrayList<App> apps = new ArrayList<App>();
        App app = null;
        InputStream inputStream=null;
        try {
            if (defaultSetting){
                inputStream = MyApplication.assetManager.open("read_setting.xml");
                Log.v("配置文件","默认");
            }
            else {
                inputStream = MyApplication.getContext().openFileInput("read_setting.xml");
                Log.v("配置文件","私有");
            }
            xpp.setInput(inputStream, "utf-8");
            int type = xpp.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                String currentTagName = xpp.getName();
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if (currentTagName.equals("app")) {
                            app = new App();
                        } else if (currentTagName.equals("id")) {
                            app.setId(xpp.nextText());
                        } else if (currentTagName.equals("ename")) {
                            app.setEname(xpp.nextText());
                        } else if (currentTagName.equals("zname")) {
                            app.setZname(xpp.nextText());
                        } else if (currentTagName.equals("uptime")) {
                            app.setUptime(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("downtime")) {
                            app.setDowntime(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("pagenumber")) {
                            app.setPagenumber(Integer.parseInt(xpp.nextText()));
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (currentTagName.equals("app")) {
                            apps.add(app);
                        }
                        break;
                    default:
                        break;
                }
                type = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return apps;
    }
    public ArrayList<App> GetXml(String path){
        XmlPullParser xpp = Xml.newPullParser();
        ArrayList<App> apps = new ArrayList<App>();
        App app = null;
        InputStream inputStream=null;
        try {
                File file = new File(path);
                inputStream = new FileInputStream(file);
                Log.v("配置文件","导入");
            xpp.setInput(inputStream, "utf-8");
            int type = xpp.getEventType();
            while (type != XmlPullParser.END_DOCUMENT) {
                String currentTagName = xpp.getName();
                switch (type) {
                    case XmlPullParser.START_TAG:
                        if (currentTagName.equals("app")) {
                            app = new App();
                        } else if (currentTagName.equals("id")) {
                            app.setId(xpp.nextText());
                        } else if (currentTagName.equals("ename")) {
                            app.setEname(xpp.nextText());
                        } else if (currentTagName.equals("zname")) {
                            app.setZname(xpp.nextText());
                        } else if (currentTagName.equals("uptime")) {
                            app.setUptime(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("downtime")) {
                            app.setDowntime(Integer.parseInt(xpp.nextText()));
                        } else if (currentTagName.equals("pagenumber")) {
                            app.setPagenumber(Integer.parseInt(xpp.nextText()));
                        }
                        break;
                    case XmlPullParser.END_TAG:
                        if (currentTagName.equals("app")) {
                            apps.add(app);
                        }
                        break;
                    default:
                        break;
                }
                type = xpp.next();
            }
        } catch (XmlPullParserException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return apps;
    }
    public boolean fileIsExists() {
        try
        {
            File f=new File(MyApplication.getContext().getFilesDir()+"/read_setting.xml");
            if(!f.exists())
            {
                return false;
            }
        }
        catch (Exception e)
        {
            return false;
        }
        return true;
    }

}
